package com.cm.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cm.common.core.Result;
import com.cm.order.entity.Orders;
import com.cm.order.entity.dto.OrdersKeyWordDTO;
import com.cm.order.mapper.OrdersMapper;
import com.cm.order.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @类 名:OrdersController </br>
 * @描 述:<br>
 * @日 期:2020/9/24 18:26 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    OrdersService ordersService;

    @PostMapping("/insert")
    public Result insert (@RequestBody Orders orders){
        if (ordersService.insert(orders)) {
            return Result.success("订单添加成功");
        }
        return Result.fail(orders.getOrderId() + "已存在！");
    }

    @PostMapping("/delete")
    public Result delete(Long orderId) {
        if (ordersService.delete(orderId)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Orders orders) {
        if (ordersService.update(orders)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @PostMapping("/list")
    public Result getOrders(@RequestBody OrdersKeyWordDTO ordersKeyWordDTO) {
        IPage<Orders> ordersIPage = ordersService.getOrderList(ordersKeyWordDTO);
        return Result.success(ordersIPage);
    }

    @GetMapping("/getById")
    public Result getById(Long order_id){
        Orders orders= ordersMapper.selectById(order_id);
        return Result.success(orders);
    }
    @PostMapping("/getByUserId")
    public Result getByUserId(Long userId){
        return Result.success(ordersService.getOrdersList(userId));
    }
}